package cn.vihub.service.impl;

import cn.vihub.param.ShopGoodsAttributeDataParam;
import cn.vihub.param.ShopGoodsParam;
import cn.vihub.param.ShopGoodsSpecDataParam;
import cn.vihub.pojo.ShopGoodsAttribute;
import cn.vihub.pojo.ShopGoodsAttributeData;
import cn.vihub.pojo.ShopGoodsSpecData;
import cn.vihub.service.ShopGoodsAttributeDataService;
import cn.vihub.service.ShopGoodsSpecDataService;
import cn.vihub.utils.Result;
import cn.vihub.vo.ShopGoodsVo;
import cn.vihub.vo.ShopPicVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.vihub.pojo.ShopGoods;
import cn.vihub.mapper.ShopGoodsMapper;
import cn.vihub.service.ShopGoodsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: 秋晚
 * @Date:  2025/7/29 20:15
 * @Description: 
 **/

@Service
@Transactional(rollbackFor = Exception.class)
public class ShopGoodsServiceImpl extends ServiceImpl<ShopGoodsMapper, ShopGoods> implements ShopGoodsService{

    @Value("${minio.imgurl}")
    private String imgurl;
    @Autowired
    ShopGoodsMapper shopGoodsMapper;

    @Autowired
    ShopGoodsSpecDataService shopGoodsSpecDataService;

    @Autowired
    ShopGoodsAttributeDataService shopGoodsAttributeDataService;
    @Override
    public IPage<ShopGoodsVo> goodsPage(ShopGoodsParam shopGoodsParam) {
        //创建分页对象
        IPage<ShopGoodsVo> page = new Page<>(shopGoodsParam.getPage(), shopGoodsParam.getLimit());
        //创建查询条件
        LambdaQueryWrapper<ShopGoodsVo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        IPage<ShopGoodsVo> shopGoodsVoIPage = shopGoodsMapper.selectGoodsPage(page, lambdaQueryWrapper);

        //遍历将字符串图片转化为图片数组
        for (ShopGoodsVo record : shopGoodsVoIPage.getRecords()) {
            ArrayList<ShopPicVo> shopGoodsVos = new ArrayList<>();
            //将字符串转化为数组
            if(record.getPic()!=null){
                String[] picStr = record.getPic().split(",");
                for (String pic : picStr) {
                    ShopPicVo shopPicVo = new ShopPicVo();
                    shopPicVo.setName(pic);
                    shopPicVo.setUrl(imgurl+pic);
                    shopGoodsVos.add(shopPicVo);
                }
                record.setPicList(shopGoodsVos);
            }
        }
        return shopGoodsVoIPage;
    }

    @Override
    public ShopGoodsVo getGoodsInfo(Integer id) {
        //根据id查询出该商品对象
        ShopGoodsVo shopGoodsVo = shopGoodsMapper.selectByGoodsId(id);

        //定义一个list用来存储图片数据
        ArrayList<ShopPicVo> shopGoodsVos = new ArrayList<>();
        //将字符串转化为数组
        if(shopGoodsVo.getPic()!=null){
            String[] picStr = shopGoodsVo.getPic().split(",");
            for (String pic : picStr) {
                ShopPicVo shopPicVo = new ShopPicVo();
                shopPicVo.setName(pic);
                shopPicVo.setUrl(imgurl+pic);
                shopGoodsVos.add(shopPicVo);
            }
            shopGoodsVo.setPicList(shopGoodsVos);
        }
        return shopGoodsVo;
    }

    @Override
    @Transactional
    public Boolean add(ShopGoodsParam shopGoodsParam) {
        //新增商品数据   获取新增后返回的商品ID
        ShopGoods shopGoods = new ShopGoods();
        String[] goodsCategoryList = shopGoodsParam.getGoodsCategoryList();
        String s = goodsCategoryList[goodsCategoryList.length - 1];
        shopGoodsParam.setGoodsCategoryId(Integer.valueOf(s));
        //拷贝赋值
        BeanUtils.copyProperties(shopGoodsParam,shopGoods);
        shopGoods.setStatus(1);

        shopGoodsMapper.insert(shopGoods);
        //新增商品+规格数据
        if(shopGoodsParam.getSpecListTable()!=null){
            ArrayList<ShopGoodsSpecData> shopGoodsSpecDatas = new ArrayList<>();
            for (ShopGoodsSpecDataParam shopGoodsSpecDataParam : shopGoodsParam.getSpecListTable()) {


                ShopGoodsSpecData shopGoodsSpecData = new ShopGoodsSpecData();
                shopGoodsSpecDataParam.setNumberCount(shopGoodsSpecDataParam.getNumber());
                if(shopGoodsSpecDataParam.getSpecStr().endsWith(",")){
                    shopGoodsSpecDataParam.setSpecStr(shopGoodsSpecDataParam.getSpecStr().substring(0,shopGoodsSpecDataParam.getSpecStr().length()-1));
                }
                BeanUtils.copyProperties(shopGoodsSpecDataParam,shopGoodsSpecData);
                shopGoodsSpecData.setGoodsId(shopGoods.getId());
                shopGoodsSpecDatas.add(shopGoodsSpecData);
            }
            shopGoodsSpecDataService.saveBatch(shopGoodsSpecDatas);
        }
        //新增商品+属性数据
        if(shopGoodsParam.getAttributeList()!=null){
            ArrayList<ShopGoodsAttributeData> shopGoodsAttributeDatas = new ArrayList<>();
            for (ShopGoodsAttributeDataParam shopGoodsAttributeDataParam : shopGoodsParam.getAttributeList()) {
                ShopGoodsAttributeData shopGoodsAttributeData = new ShopGoodsAttributeData();
                BeanUtils.copyProperties(shopGoodsAttributeDataParam,shopGoodsAttributeData);
                shopGoodsAttributeData.setShopGoodsId(shopGoods.getId());
                shopGoodsAttributeDatas.add(shopGoodsAttributeData);
            }
            shopGoodsAttributeDataService.saveBatch(shopGoodsAttributeDatas);
        }
        return true;
    }
}
